home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / npns.lha / npns / src / SendArticle.c < prev    next >
C/C++ Source or Header  |  1996-05-06  |  2KB  |  126 lines

  1.  
  2. /*
  3.  *    Function    SendArticle
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        05/05/96
  6.  *
  7.  *  Synopsis:    Sends an article to news server.
  8.  *
  9.  *  Arguments:    soc        Connected socket
  10.  *        newsfile    Filename of article to send
  11.  *
  12.  *  Returns:    int TRUE    If success
  13.  *            FALSE    If failure
  14.  *
  15.  *  Variables:    len        Amount of data read from file
  16.  *        rc        Return code
  17.  *        buf        Buffer for article
  18.  *        fptr        File pointer
  19.  * 
  20.  *  Functions:    malloc        Allocate memory (ANSI)
  21.  *        free        Free memory (ANSI)
  22.  *        fread        Read data from file (ANSI)
  23.  *        feof        Check file for EOF (ANSI)
  24.  *        fopen        Open file (ANSI)
  25.  *        fclose        Close file (ANSI)
  26.  *        fprintf        Print to stream (ANSI)
  27.  *        SendBuf        Send buffer to server (NPNS)
  28.  *        GetResponse    Get server numerical response (NNET)
  29.  *        SendCommand    Send command to server (NNET)
  30.  *        TORecv        Timed recv (NNET)
  31.  *        send        Send data (BSD)
  32.  *
  33.  *  $Id: SendArticle.c 1.1 1996/05/06 22:55:02 nagd Exp $
  34.  *
  35.  */
  36.  
  37. #include "NPNS.h"
  38.  
  39. #define BUFSIZE 10000
  40.  
  41. int SendArticle( long soc, char *newsfile ) 
  42.  
  43. {
  44.  
  45.   int len;
  46.   int rc;
  47.  
  48.   char *buf;
  49.   FILE *fptr;
  50.  
  51.   if ( buf = (char *)malloc( (BUFSIZE+1) * sizeof( char ) ) ) {
  52.  
  53.       if ( fptr = fopen( newsfile, "r" ) ) {
  54.  
  55. /*
  56.  *   Tell server we are posting and check it wants articles
  57.  */
  58.  
  59.         SendCommand( soc, "POST", NULL );
  60.  
  61.         if ( GetResponse( soc ) == 340 ) {
  62.  
  63.             TORecv( soc, buf, BUFSIZE, 0L, DEF_TIMEOUT );
  64.  
  65.  
  66. /*
  67.  *   Process file in BUFSIZE blocks
  68.  */
  69.  
  70.             while ( !feof( fptr ) ) {
  71.  
  72.                 len = fread( buf, sizeof( char ), BUFSIZE, fptr );
  73.                 buf[len] = '\0';
  74.  
  75.                 if ( len > 0 ) SendBuf( soc, buf, len );
  76.  
  77.             }   /* while */
  78.  
  79. /*
  80.  *   Terminate posting and check for acceptance
  81.  */
  82.  
  83.             send( soc, TERM_STR, TERM_LEN, 0L );
  84.  
  85.             if ( GetResponse( soc ) == 240 ) {
  86.                 rc = TRUE;
  87.              } else {
  88.                 rc = FALSE;
  89.                 fprintf( stderr, " Server did not accept article\n" );
  90.             }   /* if */
  91.  
  92.             TORecv( soc, buf, BUFSIZE, 0L, DEF_TIMEOUT );
  93.  
  94.             fclose( fptr );
  95.  
  96.         } else {
  97.  
  98.             fprintf( stderr, " Bad response to POST command\n" );
  99.             rc = FALSE;
  100.  
  101.         }   /* if */
  102.  
  103.       } else {
  104.  
  105.         fprintf( stderr, " Could not open file, %s for reading\n", newsfile );
  106.         rc = FALSE;
  107.  
  108.       }   /* if */
  109.  
  110.       free( buf );
  111.  
  112.   } else {
  113.  
  114.     fprintf( stderr, " Not enough memory !\n" );
  115.     rc = FALSE;
  116.  
  117.   }   /* if */
  118.  
  119.   return rc;
  120.  
  121. }   /* SendArticle */
  122.  
  123. /*========================================================================*
  124.                        END FUNCTION SendArticle
  125.  *========================================================================*/
  126.